package com.ptkc.ext;

import com.ptkc.core.canal.handler.EventTypeHandler;
import com.ptkc.core.esearch.model.Order;
import com.ptkc.core.esearch.repos.BusinessRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: Mr.Jiao
 * @date: 2018年11月29日
 */
@Component
@Slf4j
public class OrderStatisticsCanalHandler extends EventTypeHandler<Order> {

    @Autowired
    private BusinessRepository businessRepository;

    public OrderStatisticsCanalHandler() {
        super("runfast", "runfast_goods_sell_record");
    }

    @Override
    public Class<? extends Order> getEntryClass() {
        return Order.class;
    }

    @Override
    public boolean getSubscribe(String database, String table) {
        return this.getDatabase().equalsIgnoreCase(database) && this.getTable().equalsIgnoreCase(table);
    }


    private void recordSales(Order order){
        //统计订单商家销量
        if(order != null && order.getBusinessId() != null && order.getStatus() != null && order.getStatus().equals(8)){
            businessRepository.statisticalBusAsync(order);
        }
    }

    @Override
    public void onUpdate(Order order) {
        //统计订单商家销量
        this.recordSales(order);
    }

    @Override
    public void onInsert(Order order) {
        this.recordSales(order);
    }
}
